package com.goat.view;

import javax.swing.*;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 * 主要功能：
 * 1.嵌入面板JPanel到窗体
 * 2.监听绑定
 * 3.主线程启动
 */

public class GameJFrame extends JFrame {
    private static int X=900;
    private static int Y=600;
    private JPanel jPanel;
    public KeyListener keyListener=null;
    public MouseMotionListener mouseMotionListener;
    public MouseListener mouseListener;
    public Thread thread;


    public GameJFrame() {
        setSize(X, Y);//1.设置宽高
        setTitle("游戏测试-ppt");//2.设置界面的标题
        setAlwaysOnTop(true);//3.置顶
        setLocationRelativeTo(null);//4.居中
        setDefaultCloseOperation(EXIT_ON_CLOSE);//5.设置关闭
    }


    public void start(){
        if (jPanel!=null){
            add(jPanel);
        }
        if(keyListener!=null){
            addKeyListener(keyListener);
        }
        if (thread!=null){
            //开启游戏主线程
            thread.start();
        }
        //界面的刷新
        if(jPanel instanceof Runnable){
            //开启界面刷新repaint线程
            new Thread((Runnable) jPanel).start();
        }

        //显示界面
        this.setVisible(true);
    }




    //注入
    public void setjPanel(JPanel jPanel) {
        this.jPanel = jPanel;
    }

    public void setKeyListener(KeyListener keyListener) {
        this.keyListener = keyListener;
    }

    public void setMouseMotionListener(MouseMotionListener mouseMotionListener) {
        this.mouseMotionListener = mouseMotionListener;
    }

    public void setMouseListener(MouseListener mouseListener) {
        this.mouseListener = mouseListener;
    }

    public void setThread(Thread thread) {
        this.thread = thread;
    }
}
